package com.hs.mod_syslog.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.hs.room.model.sys.SysLog
import com.hs.room.repository.SysLogRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@HiltViewModel
class SysLogViewModel @Inject constructor(private var sysLogRepository: SysLogRepository) : ViewModel() {

    private val pageSize = 20
     var sysListData = MutableLiveData<List<SysLog>>()
    var totalLiveData = MutableLiveData<Int>()
    fun  getByCombinedConditions(  pageIndex: Int,
                                   pageSize: Int){
        viewModelScope.launch(Dispatchers.IO) {
            sysLogRepository.getByCombinedConditions(pageIndex,pageSize).collect{
                withContext(Dispatchers.Main){
                    println("${it.size}")
                    sysListData.postValue(it)
                }
            }
        }
    }

    fun  getAllCountByCombinedConditions(){
        viewModelScope.launch(Dispatchers.IO) {
           var total = sysLogRepository.getAllCountByCombinedConditions()
            withContext(Dispatchers.Main){
                totalLiveData.postValue(total)
            }
        }
    }



}